<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-8-25
 * Time: 10:13
 */

namespace common\components\services\message;

use common\components\helpers\Fun;
use common\models\datas\MessageLog;
use yii\web\NotFoundHttpException;

class SysMessage extends Message
{
    public function send($id,$site_id=null)
    {
        $item = MessageLog::findOne([$id]);
        if ($item != null && $item->receiver_arr!=null) {
            $fail_arr=[];
            $receiver=unserialize($item->receiver_arr);
            foreach($receiver as $k=>$v){
                $model = new \common\models\datas\Message();
                $model->receiver_id=$v;
                $model->site_id = $site_id;
                $model->sender_id = $item->sender_id;
                $model->content = $item->content_sys;
                $model->created_at = $item->created_at;
                $model->is_all = $item->is_all;
                $model->admin_id = $item->admin_id;
                $model->log_id = $item->id;
                $model->type_id =$item->type_id;
                $model->send_group =$item->send_group;
                if (!($model->save(false))) {
                    $fail_arr[]=$v;
                };
            }
            if(count($fail_arr)>0){
                $fail_arr_sys=serialize($fail_arr);
                $item->fail_arr_sys=$fail_arr_sys;
                $item->save(false);
                return false;
            }else{
                $log_status=unserialize($item->status);
                $log_status['sys']=1;
                $item->status = serialize($log_status);
                $item->save(false);
                return true;
            }
        }
        return false;
    }
}